package com.xhwl.centiotdata.handler.alarm.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.inject.internal.cglib.core.$LocalVariablesSorter;
import com.xhwl.centiotdata.handler.alarm.AlarmFactory;
import com.xhwl.centiotdata.handler.alarm.AlarmHandler;
import com.xhwl.common.constant.cent.alarm.AlarmConstant;
import com.xhwl.common.dto.cent.alarm.iot.AirConditionerDevice;
import com.xhwl.common.dto.cent.alarm.iot.IotAlarmBody;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.pojo.cent.alarm.AlarmManagement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;

/**
 * @Author Leon
 * @Description: TODO
 * @Date: 2022/3/22 17:31
 */
@Component
@Slf4j
public class AlarmHuaWeiIotDAAirConditionerDeviceHandler extends AlarmHandler {

    @Override
    public void afterPropertiesSet() throws Exception {
        AlarmFactory.register("AirConditionerDevice", this);
    }

    @Override
    public void pushHuaWeiIotDAIotAlarmBody(IotAlarmBody t, DeviceDTO device) {
        AirConditionerDevice airConditionerDevice = JSONObject.parseObject(JSONObject.toJSONString(t.getProperties()), AirConditionerDevice.class);
        AlarmManagement alarmManagement = new AlarmManagement();
        alarmManagement.setAlarmTime(Date.from(t.getEvent_time().atZone(ZoneId.systemDefault()).toInstant()));
        alarmManagement.setDeviceId(device.getId());
        alarmManagement.setProjectId(device.getProjectId());
        log.info("华为IOTDA推送AirConditionerDevice原始数据为=================》{}",JSONObject.toJSONString(t));
        if (Objects.equals(airConditionerDevice.getDI5(), Boolean.FALSE)) {
            alarmManagement.setAlarmInventoryCode("73560088");
            sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
        }

        if (Objects.equals(airConditionerDevice.getDI4(), Boolean.FALSE)) {
            alarmManagement.setAlarmInventoryCode("67774469");
            sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
        }

        if (Objects.equals(airConditionerDevice.getDI8(), Boolean.FALSE)) {
            alarmManagement.setAlarmInventoryCode("40700827");
            sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
        }

        if (Objects.equals(airConditionerDevice.getGZ(), Boolean.FALSE)) {
            alarmManagement.setAlarmInventoryCode("48757265");
            sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
        }

        if (Objects.equals(airConditionerDevice.getYX(), Boolean.FALSE)) {
            alarmManagement.setAlarmInventoryCode("54864541");
            sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
        }

        if(Objects.nonNull(airConditionerDevice.getFanError())){
            switch (airConditionerDevice.getFanError()) {
                case 1:
                    alarmManagement.setAlarmInventoryCode("90049045");
                    sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
                    break;
                case 2:
                    alarmManagement.setAlarmInventoryCode("01360873");
                    sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
                    break;
                case 3:
                    alarmManagement.setAlarmInventoryCode("89955240");
                    sendMq(AlarmConstant.JINMAO_TOPIC,alarmManagement);
                    break;
                default:
                    break;
            }
        }
    }
}
